package com.example.qixin0314.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.qixin0314.Activity.CompanyActivity;
import com.example.qixin0314.Nat.BaseApplication;
import com.example.qixin0314.Nat.company.StarkRequest;
import com.example.qixin0314.Nat.company.StarkValues;
import com.example.qixin0314.R;
import com.example.qixin0314.bean.FmmAdapter;
import com.example.qixin0314.bean.StarkAdapter;
import com.example.qixin0314.bean.StarkModel;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class stateFragment extends Fragment {
    private String TAG = "明星品牌";

    private ImageView noDataIcon;

    private SwipeRecyclerView starkRecyclerView;

    private SmartRefreshLayout starkEefresh;

    private int i = 1;//page 页数

    private String sort;//正序倒序

    private int DataSize;//记录页面内一共有几条数据

    private List<StarkModel> data;

    private StarkAdapter adapter;

    private LinearLayout ll_list_default;
    private TextView ll_list_default_txt;
    private ImageView ll_list_default_icon;

    private LinearLayout ll_list_brand;
    private TextView list_brand_txt;
    private ImageView ll_list_brand_icon;

    private LinearLayout list_list_type;
    private TextView list_list_type_txt;
    private ImageView list_list_type_icon;

    private RecyclerView recyclerView;
    private FmmAdapter<String> popAdapter, popAdapter1, popAdapter2;

    private List<String> popList, popList1, popList2;
    private Map<String, Integer> popMap, popMap1, popMap2;

    private PopupWindow popupWindow;
    private String currentDefault, currentBrand,currentType;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_state, container, false);
        initview(root);

        getList();
        getMap();

        InitView();
        return root;
    }

    private void initview(View root){//控件
        ll_list_default = root.findViewById(R.id.ll_list_default);
        ll_list_default_txt = root.findViewById(R.id.ll_list_default_txt);
        ll_list_default_icon = root.findViewById(R.id.ll_list_default_icon);

        ll_list_brand = root.findViewById(R.id.ll_list_brand);
        list_brand_txt = root.findViewById(R.id.list_brand_txt);
        ll_list_brand_icon = root.findViewById(R.id.ll_list_brand_icon);

        list_list_type = root.findViewById(R.id.list_list_type);
        list_list_type_txt = root.findViewById(R.id.list_list_type_txt);
        list_list_type_icon = root.findViewById(R.id.list_list_type_icon);

        ll_list_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ll_list_default_icon.setImageResource(R.mipmap.shaixuan);
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerView.setAdapter(popAdapter);
                popupWindow.showAsDropDown(ll_list_default);
            }
        });

        ll_list_brand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ll_list_brand_icon.setImageResource(R.mipmap.shaixuan);
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerView.setAdapter(popAdapter1);
                popupWindow.showAsDropDown(ll_list_default);
            }
        });

        list_list_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list_list_type_icon.setImageResource(R.mipmap.shaixuan);
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerView.setAdapter(popAdapter2);
                popupWindow.showAsDropDown(ll_list_default);
            }
        });
        //列表数据
        starkRecyclerView  = root.findViewById(R.id.stark_fragment_recyclerview);
        starkEefresh = root.findViewById(R.id.stark_smart_refresh);//刷新
        //设置列表排列方式  垂直
        starkRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        data = new ArrayList<>();
        adapter = new StarkAdapter(getActivity());
        starkAllParameterData(sort,getSdq(),getHy(),1);//首次加载
        adapter.setData(data);
        starkRecyclerView.setAdapter(adapter);
        starkRefresh();//上拉加载

        noDataIcon = root.findViewById(R.id.no_data_icon);

    }

    /**
     * 列表菜单
     */
    private void InitView() {//数据，条目
        View contentView = getLayoutInflater().inflate(R.layout.popwin_supplier_list, null);
        popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, false);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });

        recyclerView = contentView.findViewById(R.id.popwin_supplier_list_lv);
        contentView.findViewById(R.id.popwin_supplier_list_bottom).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        popAdapter = new FmmAdapter<String>(R.layout.item_listview_popwin, popList);
        popAdapter.setOnCallBackData(new FmmAdapter.OnCallBackData<String>() {
            @Override
            public void convertView(BaseViewHolder holder, String item) {
                ((TextView) holder.getView(R.id.listview_popwind_tv)).setText(item);
                if (ll_list_default_txt.getText().toString().trim().equals(item)) {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#00d8a0"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.VISIBLE);
                } else {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#333333"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.GONE);
                }
            }
        });

        popAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //后期自己做请求处理
                popupWindow.dismiss();
                currentDefault = popList.get(position);
                ll_list_default_txt.setText(currentDefault);

                if (currentDefault.equals(popList.get(position))){
                    data.clear();//清空原有数据
                    starkAllParameterData(sort,getSdq(),getHy(),0);
                }
            }
        });

        popAdapter1 = new FmmAdapter<String>(R.layout.item_listview_popwin, popList1);
        popAdapter1.setOnCallBackData(new FmmAdapter.OnCallBackData<String>() {
            @Override
            public void convertView(BaseViewHolder holder, String item) {
                ((TextView) holder.getView(R.id.listview_popwind_tv)).setText(item);
                if (list_brand_txt.getText().toString().trim().equals(item)) {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#00d8a0"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.VISIBLE);
                } else {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#333333"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.GONE);
                }
            }
        });

        popAdapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //后期自己做请求处理
                popupWindow.dismiss();
                currentBrand = popList1.get(position);
                list_brand_txt.setText(currentBrand);
                if (currentBrand.equals(popList1.get(position))){
                    data.clear();//清空原有数据
                    starkAllParameterData(sort,getSdq(),getHy(),0);
                }
            }
        });

        popAdapter2 = new FmmAdapter<String>(R.layout.item_listview_popwin, popList2);
        popAdapter2.setOnCallBackData(new FmmAdapter.OnCallBackData<String>() {
            @Override
            public void convertView(BaseViewHolder holder, String item) {
                ((TextView) holder.getView(R.id.listview_popwind_tv)).setText(item);
                if (list_list_type_txt.getText().toString().trim().equals(item)) {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#00d8a0"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.VISIBLE);
                } else {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#333333"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.GONE);
                }
            }
        });

        /**
         * 默认排序， 正序，倒序处理
         */
        popAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //后期自己做请求处理
                popupWindow.dismiss();
                currentType = popList2.get(position);
                list_list_type_txt.setText(currentType);
                if (position == 0){
                    data.clear();//清空原有数据
                    starkAllParameterData(sort,getSdq(),getHy(),0);
                }
                if (position == 1){//正序
                    data.clear();//清空原有数据
                    sort = "-ut";
                    starkAllParameterData(sort,getSdq(),getHy(),0);
                }
                if (position == 2){//倒序
                    data.clear();//清空原有数据
                    sort = "ut";
                    starkAllParameterData(sort,getSdq(),getHy(),0);
                }
                sort = "";//此处一定要写，不写会导致下次排序混乱
            }
        });
    }

    /**
     * @return ssdq_code地区参数
     */
    private int getSdq(){

        if (currentDefault==null) return 0;
        return popMap.get(currentDefault)==null?0:popMap.get(currentDefault);
    }

    /**
     * @return hylb_code行业参数
     */
    private int getHy(){

        if (currentBrand==null) return 0;
        return popMap1.get(currentBrand)==null?0:popMap1.get(currentBrand);
    }

    private void getList(){
        popList = new ArrayList<>();
        popList.add("全部地区");
        popList.add("安徽");
        popList.add("北京");
        popList.add("重庆");
        popList.add("福建");
        popList.add("广东");
        popList.add("甘肃");
        popList.add("广西");
        popList.add("贵州");
        popList.add("海南");
        popList.add("河北");
        popList.add("河南");
        popList.add("黑龙江");
        popList.add("湖北");
        popList.add("湖南");
        popList.add("吉林");
        popList.add("江苏");
        popList.add("江西");
        popList.add("辽宁");
        popList.add("内蒙古");
        popList.add("宁夏");
        popList.add("青海");
        popList.add("陕西");
        popList.add("四川");
        popList.add("山东");
        popList.add("上海");
        popList.add("山西");
        popList.add("天津");
        popList.add("新疆");
        popList.add("西藏");
        popList.add("云南");
        popList.add("浙江");
        popList.add("其他");

        popList1 = new ArrayList<>();
        popList1.add("全部行业");
        popList1.add("采矿业");
        popList1.add("制造业");
        popList1.add("电力、热力、燃气、及水生产和供应业");
        popList1.add("建筑业");
        popList1.add("批发和零售业");
        popList1.add("交通运输、仓储和邮政业");
        popList1.add("住宿和餐饮业");
        popList1.add("信息传输、软件和信息技术服务业");
        popList1.add("金融业");
        popList1.add("房地产业");
        popList1.add("租赁和商务服务业");
        popList1.add("科学研究和技术服务业");
        popList1.add("水利，环境和公共设施管理业");
        popList1.add("居民服务、修理和其他服务业");
        popList1.add("教育");
        popList1.add("卫生和社会工作");
        popList1.add("文化、体育和娱乐业");
        popList1.add("公共管理、社会保障和社会组织");
        popList1.add("国际组织");
        popList1.add("其他");

        popList2 = new ArrayList<>();
        popList2.add("默认排序");
        popList2.add("按时间正序");
        popList2.add("按时间倒序");
    }

    private void getMap(){
        popMap = new HashMap<>();
        popMap.put("全部地区",0);
        popMap.put("安徽",1);
        popMap.put("北京",2);
        popMap.put("重庆",4);
        popMap.put("福建",5);
        popMap.put("广东",6);
        popMap.put("甘肃",7);
        popMap.put("广西",8);
        popMap.put("贵州",9);
        popMap.put("海南",10);
        popMap.put("河北",11);
        popMap.put("河南",12);
        popMap.put("黑龙江",13);
        popMap.put("湖北",14);
        popMap.put("湖南",15);
        popMap.put("吉林",16);
        popMap.put("江苏",17);
        popMap.put("江西",18);
        popMap.put("辽宁",19);
        popMap.put("内蒙古",20);
        popMap.put("宁夏",21);
        popMap.put("青海",22);
        popMap.put("陕西",23);
        popMap.put("四川",24);
        popMap.put("山东",25);
        popMap.put("上海",26);
        popMap.put("山西",27);
        popMap.put("天津",28);
        popMap.put("新疆",29);
        popMap.put("西藏",30);
        popMap.put("云南",31);
        popMap.put("浙江",32);
        popMap.put("其他",33);

        popMap1 = new HashMap<>();
        popMap1.put("全部行业",0);
        popMap1.put("采矿业",1);
        popMap1.put("制造业",9);
        popMap1.put("电力、热力、燃气、及水生产和供应业",4);
        popMap1.put("建筑业",45);
        popMap1.put("批发和零售业",50);
        popMap1.put("交通运输、仓储和邮政业",53);
        popMap1.put("住宿和餐饮业",62);
        popMap1.put("信息传输、软件和信息技术服务业",65);
        popMap1.put("金融业",69);
        popMap1.put("房地产业",74);
        popMap1.put("租赁和商务服务业",76);
        popMap1.put("科学研究和技术服务业",79);
        popMap1.put("水利，环境和公共设施管理业",83);
        popMap1.put("居民服务、修理和其他服务业",87);
        popMap1.put("教育",91);
        popMap1.put("卫生和社会工作",93);
        popMap1.put("文化、体育和娱乐业",96);
        popMap1.put("公共管理、社会保障和社会组织",102);
        popMap1.put("国际组织",109);
        popMap1.put("其他",998);

        popMap2 = new HashMap<>();
        popMap2.put("默认排序",0);
        popMap2.put("按时间正序",1);
        popMap2.put("按时间倒序",2);

    }

    /**
     *  http://api.qxswt.com/index/brand?sort&ssdq_code=1&hylb_code=0&page=0
     *  sort排序参数    ssdq_code地区参数   hylb_code行业参数  page页数
     */
    private void starkAllParameterData(String sort,int ssdq_code,int hylb_code,int page){
        Call<StarkRequest<StarkValues>> call = BaseApplication.httpbinService.getStarkAllParameter(sort,ssdq_code,hylb_code,page);
        call.enqueue(new Callback<StarkRequest<StarkValues>>() {
            @Override
            public void onResponse(Call<StarkRequest<StarkValues>> call, Response<StarkRequest<StarkValues>> response) {
                for (int i = 0; i < response.body().getValues().getModel().size(); i++){

                    Log.d("品牌id",response.body().getValues().getModel().get(i).getMapping_id());

                    StarkModel starkModel = new StarkModel();
                    starkModel.setStarkIcon(response.body().getValues().getModel().get(i).getUrl());
                    starkModel.setStarkYeWu(response.body().getValues().getModel().get(i).getPinpai());
                    starkModel.setStarkName(response.body().getValues().getModel().get(i).getCompany_name());
                    starkModel.setStarkId(response.body().getValues().getModel().get(i).getMapping_id());
                    data.add(starkModel);
                }
                DataSize =  response.body().getValues().getTotalCount();
                adapter.notifyDataSetChanged();
                if (data.isEmpty()){
                    noDataIcon.setImageResource(R.mipmap.zanwushuju);
                }else {
                    noDataIcon.setImageResource(0);
                }
                adapter.setListener(new StarkAdapter.onItemViewClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        Intent intent = new Intent(getActivity(), CompanyActivity.class);
                        intent.putExtra("name",data.get(position).getStarkName());
                        intent.putExtra("uri","http://m.qxswt.com/company/"+data.get(position).getStarkId());
                        intent.putExtra("puri",data.get(position).getStarkIcon());
                        startActivity(intent);
                    }
                });
            }
            @Override
            public void onFailure(Call<StarkRequest<StarkValues>> call, Throwable t) { }
        });
    }

    private void starkRefresh(){//下拉刷新，上拉加载
        starkEefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                starkEefresh.finishLoadMore(100,true,false);
                if (DataSize>=10){//一页10条数据 此页少于10条数据 便没有下一页
                    i++;
                    starkAllParameterData(sort,getSdq(),getHy(),i);
                }else {
                    starkEefresh.finishLoadMore(50,true,true);
                }
            }

            @Override
            public void onRefresh(@NonNull  RefreshLayout refreshLayout) {
                starkEefresh.finishRefresh(50,true,false);
            }
        });
    }
}

